package wicketforge.converter;

import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleTextAttributes;

public abstract class BasePathConverter implements PathConverter {

	private String name;
	private boolean modified;

	@Override
	public String getName() {
		return name;
	}

	public void setName(final String name) {
		if (!isModified()) {
			setModified(!name.equals(this.name));
		}
		this.name = name;
	}

	@Override
	public void render(@NotNull final ColoredListCellRenderer listCellRenderer) {
		listCellRenderer.setIcon(getIcon());
		listCellRenderer.append("\t\t" + getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES, true);
		renderInternal(listCellRenderer);
	}

	protected abstract void renderInternal(@NotNull final ColoredListCellRenderer listCellRenderer);

	@Override
	public void writeToConfiguration(final Element configuration) {
		configuration.setAttribute(TYPE, getClass().getName());
		configuration.setAttribute(NAME, getName());
		writeToConfigurationInternal(configuration);
	}

	protected abstract void writeToConfigurationInternal(final Element configuration);

	@Override
	public void readFromConfiguration(final Element configuration) {
		setName(configuration.getAttributeValue(NAME));
		readFromConfigurationInternal(configuration);
	}

	protected abstract void readFromConfigurationInternal(final Element configuration);

	@Override
	public void setModified(final boolean modified) {
		this.modified = modified;
	}

	public boolean isModified() {
		return modified;
	}
}
